package com.abc.recycling.controller;

import com.abc.recycling.domain.Evaluation;
import com.abc.recycling.model.Collector;
import com.abc.recycling.model.Order;
import com.abc.recycling.model.User;
import com.abc.recycling.service.CollectorService;
import com.abc.recycling.service.CollectorOrderService;
import com.abc.recycling.service.EvaluationService;
import com.abc.recycling.service.UserService;
import com.abc.recycling.common.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/collector/profile")
@RequiredArgsConstructor
public class CollectorProfileController {

    private final UserService userService;
    private final CollectorOrderService collectorOrderService;
    private final EvaluationService evaluationService;
    private final CollectorService collectorService;

    @GetMapping
    public String getProfile(Authentication authentication, Model model) {
        try {
            log.info("获取回收员个人资料");
            // 获取当前登录的回收员信息
            User collector = userService.getUserByUsername(authentication.getName());
            log.debug("当前回收员: {}", collector.getUsername());
            model.addAttribute("collector", collector);

            // 获取订单统计
            Map<String, Long> orderStats = collectorOrderService.getCollectorOrderStats(collector.getId());
            model.addAttribute("orderStats", orderStats);
            log.debug("订单统计: {}", orderStats);

            // 获取评价统计
            Map<String, Object> evaluationStats = collectorOrderService.getCollectorEvaluationStats(collector.getId());
            model.addAttribute("evaluationStats", evaluationStats);
            log.debug("评价统计: {}", evaluationStats);

            log.info("回收员个人资料获取成功");
            return "collector/profile :: content";
        } catch (Exception e) {
            log.error("获取回收员个人资料失败: {}", e.getMessage());
            throw e;
        }
    }

    @GetMapping("/orders/history")
    public String getOrderHistory(
            @RequestParam Long collectorId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            Model model) {
        try {
            log.info("获取回收员订单历史, collectorId={}, page={}, size={}", collectorId, page, size);
            PageRequest pageRequest = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createTime"));
            Page<Order> orders = collectorOrderService.getCollectorOrderHistory(collectorId, pageRequest);
            model.addAttribute("orders", orders);
            log.debug("订单历史记录数: {}", orders.getTotalElements());
            return "collector/order-history";
        } catch (Exception e) {
            log.error("获取回收员订单历史失败: {}", e.getMessage());
            throw e;
        }
    }

    @PostMapping("/update")
    @ResponseBody
    public Result<?> updateProfile(@RequestBody Collector updatedCollector) {
        try {
            log.info("更新回收员资料, collectorId={}", updatedCollector.getId());
            collectorService.updateCollector(updatedCollector);
            log.info("回收员资料更新成功");
            return Result.success("更新成功");
        } catch (Exception e) {
            log.error("更新回收员资料失败: {}", e.getMessage());
            return Result.failed(e.getMessage());
        }
    }
} 